package com.example.tests;

import static com.example.tests.ContactDataGenerator.generateRandomContacts;
import static com.example.tests.GroupDataGenerator.generateRandomGroups;

import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;

import com.example.fw.ApplicationManager;

public class TestBase {

	protected static ApplicationManager app;
	public static int COUNT_OBJECTS = 1; 

	@BeforeTest
	public void setUp() throws Exception {
		String conigFile = System.getProperty("configFile", "application.properties");
		Properties properties = new Properties();
		properties.load(new FileReader(new File(conigFile)));
		app = new ApplicationManager(properties);
	}
	
	@AfterTest
	public void tearDown() throws Exception {
		app.stop();
	}
	 
	@DataProvider
	public Iterator<Object[]> randomValidGroupGenerator(){
		return wrapDataForDataProvider(generateRandomGroups(COUNT_OBJECTS)).iterator();
	}
	
	@DataProvider
	public Iterator<Object[]> randomValidContactGenerator(){		 
		return wrapDataForDataProvider(generateRandomContacts(COUNT_OBJECTS)).iterator();
	}
	
	public static List<Object[]> wrapDataForDataProvider(List<?> contacts) {
		List<Object[]> list = new ArrayList<Object[]>();
		for (Object contact: contacts) {
			list.add(new Object[]{contact});
		}
		return list;
	}	  
}
